package com.example.springworkadmin.controller;

import com.example.springworkadmin.dao.UserAccountDao;
import com.example.springworkadmin.entity.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/selfAccount")
public class selfAccountController {
    @Autowired
    UserAccountDao userAccountDao;

    @GetMapping("/updatePage")
    public String getPage(){
        return "selfManage/updateSelfAccount";
    }

    /**
     * 更新密码（后台）
     * @param password 原密码
     * @param rePassword 新密码
     * @return code:0代表成功
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Integer> update(Principal principal,@RequestParam("password")String password, @RequestParam("rePassword")String rePassword){
        Integer flag=0;
        HashMap<String,Integer> map = new HashMap<>();
        UserAccount userAccount=userAccountDao.getById(principal.getName());
        if(!userAccount.getPassword().equals(password)){
            map.put("code",flag-1);
            return map;
        }
        try {
            userAccount.setPassword(rePassword);
            flag=userAccountDao.update(userAccount);
        }catch (Exception e){
            System.out.println("更新密码失败");
        }
        map.put("code",flag-1);
        return map;
    }
}
